package com.service.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.order.client.CourseClient;
import com.service.order.client.TeacherClient;
import com.service.order.client.UcenterMemberClient;
import com.service.order.pojo.Order;
import com.service.order.mapper.OrderMapper;
import com.service.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.service.order.service.PayLogService;
import com.service.order.utils.OrderNoUtil;
import com.service.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-10
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private TeacherClient teacherClient;

    @Autowired
    private UcenterMemberClient ucenterMemberClient;

    @Autowired
    private PayLogService payLogService;

    @Override
    public ResultVo queryById(String orderId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_no",orderId);
        Order order = null;
        try {
            order = this.getOne(queryWrapper);
            ResultVo resultVo = courseClient.queryById(order.getCourseId());
            Map<String, Object> map = JSONObject.parseObject(JSON.toJSON(resultVo.getData().get("item")).toString(), HashMap.class);

            order.setCourseTitle(map.get("title").toString());
            order.setCourseCover(map.get("cover").toString());
            resultVo = teacherClient.queryById(order.getTeacherId());
            map = JSONObject.parseObject(JSON.toJSON(resultVo.getData().get("item")).toString(), HashMap.class);

            order.setTeacherName(map.get("name").toString());

            resultVo = ucenterMemberClient.queryById(order.getMemberId());
            map = JSONObject.parseObject(JSON.toJSON(resultVo.getData().get("item")).toString(), HashMap.class);
            order.setNickname(map.get("nickname").toString());
            order.setMobile(map.get("mobile").toString());

        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.error();
        }

        return ResultVo.ok().data("item",order);

    }

    @Override
    public ResultVo saveOrder(Order order) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",order.getCourseId());
        queryWrapper.eq("member_id",order.getMemberId());
        Order one = this.getOne(queryWrapper);
        if (one!=null){
            return ResultVo.ok().data("orderId", one.getOrderNo());
        }
        String orderNo = OrderNoUtil.getOrderNo();
        try {

            order.setOrderNo(orderNo);
            order.setGmtCreate(new Date());
            order.setGmtModified(new Date());
            order.setStatus(0);
            order.setPayType(0);
            this.save(order);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVo.error();
        }

        return ResultVo.ok().data("orderId",orderNo);
    }
}
